package com.guli.gulimall.product.web.controller;

import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
public class HiController {

    @Autowired
    private RedissonClient client;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/hello")
    public String hello(){
        //获取锁
        RLock mylock = client.getLock("mylock");
        //加锁  //默认过期时间30s
        //自动续期 30-->20s 如果业务没有执行结束 再续期到30s
        // lockWatchdogTimeout 看门狗 默认30s
        mylock.lock();
        try {
            //执行业务
            System.out.println(Thread.currentThread().getId()+"执行业务");
            Thread.sleep(50000);//业务执行50s
            return "ok";
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //释放锁
            System.out.println(Thread.currentThread().getId()+"释放锁。。。。");
            mylock.unlock();
        }
        return "error";
    }

    //写锁
    @GetMapping("/write")
    public String write(){
        //获取读写锁
        RReadWriteLock wrlock = client.getReadWriteLock("wrlock");
        RLock writeLock = wrlock.writeLock();
        //写锁
        writeLock.lock();
        String s = "";
        try {
            //执行业务
            System.out.println(Thread.currentThread().getId()+"写锁执行业务");
            s = UUID.randomUUID().toString().replace("-","");
            redisTemplate.opsForValue().set("writeValue",s,5, TimeUnit.MINUTES);
            Thread.sleep(5000);//业务执行50s
            return s;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //释放锁
            System.out.println(Thread.currentThread().getId()+"写锁释放锁。。。。");
            writeLock.unlock();
        }

        return s;
    }

    /**
     * 读锁+ 读锁 相当于无锁
     *  读 + 写  先读 再写 阻塞
     *  写 + 读 先写 再读
     *  写+ 写   阻塞等待
     * @return
     */
    //读锁
    @GetMapping("/read")
    public String read(){
        //获取读写锁
        RReadWriteLock wrlock = client.getReadWriteLock("wrlock");
        RLock readLock = wrlock.readLock();
        //写锁
        readLock.lock();
        String s = "";
        try {
            //执行业务
            System.out.println(Thread.currentThread().getId()+"读锁执行业务");

            s = redisTemplate.opsForValue().get("writeValue");

            Thread.sleep(5000);//业务执行50s
            return s;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //释放锁
            System.out.println(Thread.currentThread().getId()+"读锁释放锁。。。。");
            readLock.unlock();
        }

        return s;
    }

    //停车
    @GetMapping("/park")
    public String park(){
        //获取信号量
        RSemaphore park = client.getSemaphore("park");
        try {
            //尝试获取信号量
            boolean b = park.tryAcquire();


            if(b){
                System.out.println(Thread.currentThread().getId()+"停车成功");
                return "ok";
            }else{
                System.out.println(Thread.currentThread().getId()+"停车失败");
                return "error";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    //离开
    @GetMapping("/go")
    public String go(){
        //获取信号量
        RSemaphore park = client.getSemaphore("park");
        try {//尝试获取信号量
            park.release();
            System.out.println(Thread.currentThread().getId()+"离开了");
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }
    //分布式闭锁
    @GetMapping("lockDoor")
    public String lockDoor() throws InterruptedException {

        RCountDownLatch door = client.getCountDownLatch("door");

        door.trySetCount(5);
        door.await();//阻塞等待
        return "放假了";
    }

    //放假班级走
    @GetMapping("/gogogo/{id}")
    public String gogogo(@PathVariable("id") String id){
        RCountDownLatch door = client.getCountDownLatch("door");
        door.countDown();//计数
        System.out.println(id+"班级走了"+Thread.currentThread().getId());
        return id+"班级走了";
    }


}
